<?php 
// no direct access
$layout = JRequest::getVar('layout', 'jalist');
//if($layout!='jalist') return ;
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.model' );
require_once JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'asset'.DS.'jaconstants.php';
require_once JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'asset'.DS.'japermissions.php';
require_once(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'helper'.DS.'jafunctions.class.php');
require_once(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'models'.DS.'jajobs.php');

global $jbconfig;
component_configs();
//checkPermission('view_list_jobs');

 
// Include the syndicate functions only once
if(!defined('JAJOBBOARD_GLOBAL_CSS')){
	 global $mainframe;
	 if(file_exists(JPATH_BASE.DS.'templates/'.$mainframe->getTemplate().'/css/ja.jobboard.css')){
	 	JHTML::stylesheet('ja.jobboard.css', 'templates/'.$mainframe->getTemplate().'/css/');
	 }
	 else{
	 	JHTML::stylesheet('ja.jobboard.css', 'components/'. 'com_jajobboard' .'/css/');
	 }    	 	 	 
	 JHTML::script('ja.jobboard.js', 'components/com_jajobboard/js/');
	 JHTML::script('joomla.javascript.js', 'includes/js/');
	 
	 define('JAJOBBOARD_GLOBAL_CSS', 1);
}  
if(!defined('JAJOBBOARD_MOD_FILTERJOBS_JS')){	    
	JHTML::script('ja.filterjobs.js', 'modules/mod_jafilter_jobs/ja_filter_jobs/js/');
	define('JAJOBBOARD_MOD_FILTERJOBS_JS', 1);		
   
}
require_once (dirname(__FILE__).DS.'helper.php');
$modHelper = new modJAFilterJobsHelper();
JHTML::stylesheet('', $modHelper->getFile('ja.job_filter.css','modules/mod_jafilter_jobs/ja_filter_jobs/css/','templates/'.$mainframe->getTemplate().'/css/'));


/**
* Requirement Cache 
* */

require_once(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'asset'.DS.'jacache'.DS.'jacache.php');
require_once(JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'asset'.DS.'jacache'.DS.'jajob.inc.php');

$cache = new JA_Cache($jbconfig['cache']->get('cache_base', JPATH_SITE.DS.'components'.DS.'com_jajobboard'.DS.'cache'), $jbconfig['cache']->get('cache_time', 5), $jbconfig['cache']->get('caching', true));
$name = 'mod_jafilter_jobs';
$user = JFactory::getUser();
if($user->id) $name .= '_'.$user->id;
$cache_id = md5($name);
$cache_group = 'jajobs';
if($cache->check_cache($cache_id, $cache_group)) return ;
else $oldlen = strlen(ob_get_contents());
/* End Cache */


$param['filter_fields'] = $params->get('list_filter');
$param['group_height'] = $params->get('group_height');
$param['single_mode'] = $params->get('single_mode');
$param['display_order'] = str_replace(" ", "", $params->get('display_order'));
$param['display_order'] = explode(',',$param['display_order']);
$param['popup_position'] = $params->get('popup_position', "right");
$param['number_items'] = $params->get('number_items', 5);
$enabled_salary_field = false;

if ($param['filter_fields'])
{
	//Prepare order display field - Begin -
	$temp_fieldList = array();
	
	
	for ($i=0;$i<count($param['display_order']);$i++)
	{
		if (isset($param['filter_fields'][$param['display_order'][$i]-1]) && $param['filter_fields'][$param['display_order'][$i]-1]>0)
			$temp_fieldList[] = $param['filter_fields'][$param['display_order'][$i]-1];
		
	}
	if(is_array($param['filter_fields'])){
		for ($i=0;$i<count($param['filter_fields']);$i++){
			if (!in_array($param['filter_fields'][$i], $temp_fieldList) && $param['filter_fields'][$i]!='salary'){
				$temp_fieldList[] = $param['filter_fields'][$i];
			}
			
			if($param['filter_fields'][$i]=='salary'){
				$enabled_salary_field = true;
				unset($param['filter_fields'][$i]);
			}
		}
	}
	elseif(intval($param['filter_fields'])){
		$temp_fieldList[] = $param['filter_fields'];
	}
	//Prepare order display field - End -
}


if (!isset($param['popup_position'])||$param['popup_position']=='') $param['popup_position'] = 'right';
global $mainframe;

$model = new JobBoardModelJAJobs();


$filter_fields = array();
if($param['filter_fields']!='')
{
	if (is_array($param['filter_fields']))
		$fids=implode(',', $param['filter_fields']);
	else 
		$fids = $param['filter_fields'];
		$fieldEditList = $model->getEditFieldList(" and f.frontend_hidden=0 and f.id in ($fids) and f.is_on_filter=1");
		
		//Order display field - Begin -
		for ($i=0;$i<count($temp_fieldList);$i++)
		{
			for ($j=0;$j<count($fieldEditList);$j++){
				if ($temp_fieldList[$i]==$fieldEditList[$j]->id){
					$temp_fieldList[$i] = $fieldEditList[$j];
				}
			}
		}
		$fieldEditList = $temp_fieldList;
		//Order display field - End -
		$filter_fields = $modHelper->getValues($fieldEditList);	
		
}
else	$fieldEditList = '';


$Itemid = get_Itemid(array('option'=>'com_jajobboard', 'view'=>'jajobs', 'layout'=>'jalist'));
	
$path = JModuleHelper::getLayoutPath('mod_jafilter_jobs', 'default');
if (file_exists($path)) {
	require($path);
}

/* Set Cache */
$maincontent = substr(ob_get_contents(), $oldlen);
if($maincontent){
	$cache->_cache->store($maincontent, $cache_id, $cache_group);		
}
